package nl.sogyo.objectvisualisatie.GUI;

import java.awt.BorderLayout;
import java.util.Map.Entry;

import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import nl.sogyo.objectvisualisatie.core.Model;
import nl.sogyo.objectvisualisatie.data.SourceCode;

/**
 * This class is responsible for visualising the source code
 * of the application. Each sourcefile will be shown in a different tab.
 * @author Ben
 *
 */
@SuppressWarnings("serial")
public class SourceView extends JPanel implements Observer {
	
	private JList panelList;	   
    private DefaultListModel panelModel;
    private JTabbedPane tabbedPane;
    private Model model;
	
	public SourceView()
	{
		super(new BorderLayout());
		tabbedPane = new JTabbedPane();
		tabbedPane.setFocusable(false);
		this.add(tabbedPane, BorderLayout.CENTER);
    }
    
    /** 
	 * @return = an ImageIcon, or null if the path was invalid.
	 */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = SourceView.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    /**
     * Initialise the model class of the application
     * @param model = model of the application (core functionality)
     */
    public void initalise(Model model) {
		this.model = model;
	}
    
    /**
     * 
     */
    public void update() 
	{	
    	if(model.readAllSourcefiles().size() == 0) tabbedPane.removeAll();
    	else if(tabbedPane.getComponentCount() != model.readAllSourcefiles().size())
    	{
    		tabbedPane.removeAll();
			for(Entry<String, SourceCode> entry: model.readAllSourcefiles().entrySet()) 
		    {	
		    	addTab(entry);
			}
    	}
	}
    
    /**
     * Method for adding a tab with filename and source code to the tabbedPane
     * @param entry = key-value pair with filename as key and sourcecode as value
     */
    private void addTab(Entry<String, SourceCode> entry)
    {
    	String key = entry.getKey();
    	SourceCode value = entry.getValue();
		panelModel = new DefaultListModel();
		panelList = new JList(panelModel);
	    
		JScrollPane panelListScrollPane = new JScrollPane(panelList,
		        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
		        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		if(value.showAll().size() > 0) //If 0 lines of code file failed to load
		{				
	        int lineNr = 1;
	        int i = 0;
	        String indent = "";
	        for(String str: value.showAll())
			{
		        indent = "";
		        if(str.contains("}")) i--;
	        	for(int j = 0; j < i; j++) 
	        	{
	        		indent += "    ";
	        	}				
				panelModel.addElement(lineNr + ": "+ indent + str);
				if(str.contains("{")) i++;
				lineNr++;
			}
	        tabbedPane.addTab(key, null, panelListScrollPane,"Source of "+key);
		}
		else
		{
			panelModel.addElement("Error reading source file");
			tabbedPane.addTab(key, null, panelListScrollPane,"Source of "+key);
		}
    }
     
}

